<template>
  <div class="mhy-container mhy-account-center-content">
    <div class="mhy-account-center-content-container mhy-account-center-collection">
      <div class="mhy-account-center__subheader">
        <span>个人简介</span>
        <div class="mhy-account-center-collection-menu">
          <div class="mhy-button mhy-account-center-collection-menu__create mhy-button-outlined">
            <button class="mhy-button__button" @click="updateModal">编辑</button>
          </div>
          <!---->
        </div>
      </div>
    </div>
    <div style="margin: 20px;">
      <el-descriptions class="margin-top" :column="3" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            头像
          </template>
          <div>
            <el-image :src="form.avatar" style="width: 50px;height: 50px;"></el-image>
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            手机号
          </template>
          {{form.mobile}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user-solid"></i>
            昵称
          </template>
          {{ form.nickname }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            年龄
          </template>
          <el-tag size="small">{{form.age==null?'保密':form.age}}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            性别
          </template>
          <el-tag size="small">
            {{form.sex==0?'保密':form.sex==1?'女':form.sex==2?'男':'未知'}}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            邮箱Email
          </template>
          {{form.email==null?'未绑定邮箱':form.email}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            用户签名
          </template>
          {{form.sign==null?'还没有输入签名哦':form.sign}}
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <!--编辑窗口-->
    <el-dialog
      title="修改信息"
      @closed="clos"
      :visible.sync="box"
      v-loading="loading"
      width="50%"
      :close-on-click-modal="true"
      center>
      <div>
        <el-form status-icon
                 :rules="rules"
                 ref="form"
                 :model="form"
                 label-width="120px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="头像：">
                <el-upload
                  class="avatar-uploader"
                  action="#"
                  :show-file-list="false"
                  :on-change="handleChange"
                  :auto-upload="false"
                >
                  <img v-if="form.avatar" :src="form.avatar" class="avatar" alt="">
                  <i v-else class="el-icon-plus avatar-uploader-icon" />
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="昵称" prop="nickname">
                <el-input v-model="form.nickname" placeholder="请输入昵称" clearable>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-radio-group v-model="form.sex">
                  <el-radio :label="0">保密</el-radio>
                  <el-radio :label="1">女</el-radio>
                  <el-radio :label="2">男</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱Email" prop="hobby">
                <el-input v-model="form.email" placeholder="请输入邮箱" clearable>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="年龄" prop="job">
                <el-input v-model="form.age" placeholder="请输入年龄" clearable>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="个人签名" prop="remark">
                <el-input type="textarea" resize="none" v-model="form.sign" placeholder="请输入个人签名" clearable>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div style="text-align: center;">
            <el-button type="primary" style="width: 100px;" @click="submitFun">提交</el-button>
            <el-button type="primary" plain style="width: 100px;" @click="box=false">取消</el-button>
          </div>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import cookie from "js-cookie";
import memberApi from '@/api/member'
export default {
  data() {
    return {
      loading: false,
      box: false,
      form: {
        avatar:'',//回显头像
        nickname: '',
        gender: '',
        hobby:'',
        job:'',
        remark: ''
      },
      file:null,
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'change' }
        ],
        gender: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ]
      },
    };
  },
  methods: {
    //关闭编辑时的回调
    clos(){
      this.cre()
    },
    handleChange(file){
      this.file = file.raw;
      this.form.avatar = URL.createObjectURL(file.raw);

    },
    updateModal(){//打开窗口
      // if(this.$refs.form){
      //   this.$refs.form.resetFields();
      // }
      // this.form.gender = this.form.gender + '';
      // console.info(this.form);
      this.box = true;
    },
    submitFun(){//提交
      var formData = new FormData();
      var data = this.file;
      for (let key in data){
        formData.append(key,data[key])
      }
      memberApi.upAva(formData)
        .then(response => {
          console.log(response)
          memberApi.getUpProfile(this.form)
            .then(response => {
              if (response.data.data){
                this.$message({
                  type: 'success',
                  message: '修改成功!'
                })
                this.$parent.showInfo();
                // this.cre()
                this.box = false;
              }else {
                this.$message({
                  type: 'error',
                  message: '修改失败!'
                })
              }
            })
        })

    },
    cre(){
      //从cookie获取用户信息
      var userStr = cookie.get('front_member')
      // 把字符串转换json对象(js对象)
      if(userStr) {
        this.form = JSON.parse(userStr)
        console.log(this.form)
      }
    }
  },
  created() {
   this.cre()
  }
};
</script>

<style scoped>
.mhy-account-center-content {
  width: 700px;
  float: right;
}
.mhy-container {
  background-color: #fff;
  border-radius: 4px;
}
p{
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  outline: none;
}
.mhy-account-center__subheader {
  padding: 0 30px;
  line-height: 50px;
  border-bottom: 1px solid #ebebeb;
  font-size: 16px;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.mhy-account-center-collection-menu {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  font-size: 14px;
}
.mhy-account-center-collection-menu .mhy-button {
  height: 28px;
  line-height: 28px;
  font-weight: 600;
}
.mhy-account-center-collection-menu__create {
  width: 88px;
}
.mhy-button {
  display: inline-block;
  cursor: pointer;
  -ms-flex-negative: 0;
  flex-shrink: 0;
}
.mhy-button-outlined .mhy-button__button {
  background-color: #fff;
  color: #00b2ff;
  border: 1px solid #00c3ff;
  border-radius: 4px;
  -webkit-transition-duration: .2s;
  -o-transition-duration: .2s;
  transition-duration: .2s;
  -webkit-transition-property: border-color,color;
  -o-transition-property: border-color,color;
  transition-property: border-color,color;
}
.mhy-button__button {
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  cursor: pointer;
  outline: none;
  font-size: inherit;
  color: inherit;
  width: 100%;
  height: 100%;
  background-color: transparent;
  border: none;
  border-radius: 0;
  font-weight: inherit;
  line-height: inherit;
}
</style>

